﻿using AutoMapper;
using Common.Property;
using Common.Utility;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BaseController : ControllerBase
    {
        public HttpContext _HttpContext { get; set; }
        public RedisUtil _redisUtil { get; set; }
        public IMapper _mapper { get; set; }

        public BaseController()
        {
        }

        public BaseController(IHttpContextAccessor httpContextAccessor, RedisUtil redisUtil, IMapper mapper)
        {
            _HttpContext = httpContextAccessor.HttpContext;
            _redisUtil = redisUtil;
            _mapper = mapper;
        }

        public string SitePath { get { return AppSetting.GetAppSetting("SitePath"); } }
        

        public OperatorInfo OperatorInfo
        {
            get
            {
                return new OperatorInfo
                {
                    UserId = LoginContext?.MemberId
                };
            }
        }

        public string Token => _HttpContext.Request.Headers["token"];

        private LoginContext _loginContext;
        public LoginContext LoginContext
        {
            get
            {
                if (_loginContext != null)
                {
                    return _loginContext;
                }
                if (string.IsNullOrWhiteSpace(Token))
                {
                    return null;
                }
                _loginContext = _redisUtil.Get<LoginContext>($"LoginContext.{Token}");
                return _loginContext;
            }
        }
    }
}